 package com.example.research.service;

import com.example.research.entity.HorizontalProject;
import com.example.research.mapper.HorizontalProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 横向课题服务类
 */
@Service
@Transactional
public class HorizontalProjectService {

    @Autowired
    private HorizontalProjectMapper horizontalProjectMapper;

    /**
     * 根据ID查询横向课题
     */
    public HorizontalProject findById(Long id) {
        return horizontalProjectMapper.selectById(id);
    }

    /**
     * 查询所有横向课题
     */
    public List<HorizontalProject> findAll() {
        return horizontalProjectMapper.selectAll();
    }

    /**
     * 根据用户ID查询横向课题
     */
    public List<HorizontalProject> findByUserId(Long userId) {
        return horizontalProjectMapper.selectByUserId(userId);
    }

    /**
     * 根据审核状态查询横向课题
     */
    public List<HorizontalProject> findByAuditStatus(Integer auditStatus) {
        return horizontalProjectMapper.selectByAuditStatus(auditStatus);
    }

    /**
     * 根据甲方单位查询横向课题
     */
    public List<HorizontalProject> findByPartyName(String partyName) {
        return horizontalProjectMapper.selectByPartyName(partyName);
    }

    /**
     * 根据合同金额范围查询横向课题
     */
    public List<HorizontalProject> findByAmountRange(BigDecimal minAmount, BigDecimal maxAmount) {
        return horizontalProjectMapper.selectByAmountRange(minAmount, maxAmount);
    }

    /**
     * 根据时间范围查询横向课题
     */
    public List<HorizontalProject> findByDateRange(LocalDate startDate, LocalDate endDate) {
        return horizontalProjectMapper.selectByDateRange(startDate, endDate);
    }

    /**
     * 根据条件查询横向课题
     */
    public List<HorizontalProject> findByConditions(Long userId, String partyName, 
                                                   Integer auditStatus, LocalDate startDate, LocalDate endDate) {
        return horizontalProjectMapper.selectByConditions(userId, partyName, auditStatus, startDate, endDate);
    }

    /**
     * 创建横向课题
     */
    public HorizontalProject createProject(HorizontalProject project) {
        // 设置默认审核状态为待审核
        if (project.getAuditStatus() == null) {
            project.setAuditStatus(0);
        }

        horizontalProjectMapper.insert(project);
        return project;
    }

    /**
     * 更新横向课题
     */
    public HorizontalProject updateProject(HorizontalProject project) {
        HorizontalProject existingProject = horizontalProjectMapper.selectById(project.getId());
        if (existingProject == null) {
            throw new RuntimeException("横向课题不存在");
        }

        // 检查是否可以修改
        if (!existingProject.canModify()) {
            throw new RuntimeException("审核通过的课题不能修改");
        }

        // 检查权限：只能修改自己的课题
        if (!existingProject.getUserId().equals(project.getUserId())) {
            throw new RuntimeException("只能修改自己的课题");
        }

        horizontalProjectMapper.update(project);
        return horizontalProjectMapper.selectById(project.getId());
    }

    /**
     * 删除横向课题
     */
    public void deleteProject(Long id, Long userId) {
        HorizontalProject project = horizontalProjectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException("横向课题不存在");
        }

        // 检查是否可以删除
        if (!project.canModify()) {
            throw new RuntimeException("审核通过的课题不能删除");
        }

        // 检查权限：只能删除自己的课题
        if (!project.getUserId().equals(userId)) {
            throw new RuntimeException("只能删除自己的课题");
        }

        horizontalProjectMapper.deleteById(id);
    }

    /**
     * 更新审核状态
     */
    public void updateAuditStatus(Long id, Integer auditStatus, String auditComment) {
        HorizontalProject project = horizontalProjectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException("横向课题不存在");
        }

        horizontalProjectMapper.updateAuditStatus(id, auditStatus, auditComment);
    }

    /**
     * 提交审核
     */
    public void submitForAudit(Long id, Long userId) {
        HorizontalProject project = horizontalProjectMapper.selectById(id);
        if (project == null) {
            throw new RuntimeException("横向课题不存在");
        }

        // 检查权限
        if (!project.getUserId().equals(userId)) {
            throw new RuntimeException("只能提交自己的课题");
        }

        // 检查当前状态
        if (project.getAuditStatus() == 1) {
            throw new RuntimeException("课题已审核通过，无需重复提交");
        }

        // 更新为待审核状态
        horizontalProjectMapper.updateAuditStatus(id, 0, null);
    }
}
